package cn.imvc.app.controllers.goods;



import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import cn.imvc.app.entities.PriceClass;
import cn.imvc.data.domain.ViewRequest;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.app.models.PriceClassModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
/**
 * 
 * @author zhy
 * @date 2016年7月20日上午11:18:09
 *
 */
public class PriceClassControllerImpl implements PriceClassController {
	static Logger logger = Logger.getLogger(PriceClassControllerImpl.class);
	public Page<PriceClass> page;
	public ViewRequest viewRequest;
	public PriceClass priceClass;
	public int id;
	public int[] ids;
	
	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public void setPriceClass(PriceClass priceClass) {
		this.priceClass = priceClass;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setIds(int[] ids) {
		this.ids = ids;
	}
  @Autowired
  PriceClassModel priceClassModel;
  @Autowired
	LoggingModel loggingModel;
	public String search() {
		
		return "price-class/search";
	}

	public String index() {
		if(viewRequest==null){
			viewRequest = new ViewRequest();
		}
		viewRequest.getSort().put("sort", "ASC");
		page = priceClassModel.find(viewRequest);
		return "price-class/index";
	}

	public String show() {
		priceClass = priceClassModel.get(id);
		return "price-class/edit";
	}

	public String editNew() {
		
		return "price-class/edit";
	}

	public String create() {
	
		int result = priceClassModel.save(priceClass);
		if(result ==0){
		 return Render.json(new DialogBean("300","价格编码不能重复","price-class","closeCurrent"));
		}
		if(result ==-1){
		 return Render.json(new DialogBean("300","价格名称不能重复","price-class","closeCurrent"));
		}
		loggingModel.info(logger,"添加价格："+priceClass.getName());
		return Render.json(new DialogBean("200","创建成功","","closeCurrent"));
	}

	public String edit() {
		priceClass = priceClassModel.get(id);
		return "price-class/edit";
	}

	public String update() {
	 
		int result = priceClassModel.update(id,priceClass);
		if(result ==0){
		return Render.json(new DialogBean("300","价格编码不能重复","price-class","closeCurrent"));
		}
		if(result ==-1){
		 return Render.json(new DialogBean("300","价格名称不能重复","price-class","closeCurrent"));
		}
		loggingModel.info(logger,"修改价格："+priceClass.getName());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		String result = priceClassModel.delete(id);
		if(result.equals("")){
			loggingModel.info(logger,"删除价格："+id);
		    return Render.json(new DialogBean("200","删除成功",""));
		}else{
			return Render.json(new DialogBean("300",result,""));
		}
	}

	public String destroyBatch() {
		priceClassModel.deleteBatch(ids);
		return Render.json(new DialogBean("200","删除成功",""));
	}

}
